package com.app.shop.demo.fragments;


import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.fragment.app.Fragment;

import com.app.shop.demo.R;
import com.app.shop.demo.activitys.SearchActivity;
import com.app.shop.demo.adapters.HomeListAdapter;
import com.app.shop.demo.beans.GoodsBean;
import com.app.shop.mylibrary.MyWebActivity;
import com.app.shop.mylibrary.base.BaseFragment;
import com.app.shop.mylibrary.widgts.ScrollListView;
import com.facebook.drawee.view.SimpleDraweeView;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.listener.OnBannerListener;
import com.youth.banner.loader.ImageLoader;

import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * A simple {@link Fragment} subclass.
 */
public class HomeFragment extends BaseFragment {


    @BindView(R.id.banner)
    Banner banner;
    @BindView(R.id.imgv_new_icon)
    ImageView imgvNewIcon;
    @BindView(R.id.tv_new_content)
    TextView tvNewContent;
    @BindView(R.id.imgv_new_arrow)
    ImageView imgvNewArrow;
    @BindView(R.id.rela_new_event)
    RelativeLayout relaNewEvent;
    @BindView(R.id.lv)
    ScrollListView lv;

    private List<GoodsBean> list  ;
    private HomeListAdapter adapter;

    public HomeFragment() {
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_home, container, false);
        ButterKnife.bind(this, view);

        lv.setFocusable(false);
        lv.setFocusableInTouchMode(false);
        initBanner();
        initList();
        return view;
    }

    //列表
    private void initList() {
        list = DataSupport.findAll(GoodsBean.class);
        if (list.size() == 0) {


            for (int i = 0; i < 5; i++) {
                GoodsBean bean = new GoodsBean();
                bean.setGoods_id(i);
                bean.setGoods_num(1);
                bean.setGoods_price(10 + (i * 5));

                String name = "";
                int pic = 0;
                if (i % 5 == 1) {
                    name = "中华上下五千年";
                    pic = R.mipmap.icon_book1;
                } else if (i % 5 == 2) {
                    name = "明朝那些事儿";
                    pic = R.mipmap.icon_book2;
                } else if (i % 5 == 3) {
                    name = "厚黑学";
                    pic = R.mipmap.icon_book3;
                } else if (i % 5 == 4) {
                    name = "格局";
                    pic = R.mipmap.icon_book4;
                } else {
                    name = "其他好书";
                    pic = R.mipmap.icon_book5;
                }

                bean.setGoods_name(name);
                bean.setGoods_pic(pic);
                list.add(bean);
            }

            DataSupport.saveAll(list);
        }

        adapter = new HomeListAdapter(getContext(), (ArrayList) list, R.layout.item_home);
        lv.setAdapter(adapter);

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                //示例链接，随意修改
                String url = "";
                if (position % 5 == 1) {
                    url = "https://baike.baidu.com/item/%E4%B8%AD%E5%9B%BD%E4%B8%8A%E4%B8%8B%E4%BA%94%E5%8D%83%E5%B9%B4/3019195?fr=aladdin";
                } else if (position % 5 == 2) {
                    url = "https://baike.baidu.com/item/%E6%98%8E%E6%9C%9D%E9%82%A3%E4%BA%9B%E4%BA%8B%E5%84%BF";
                } else if (position % 5 == 3) {
                    url = "https://baike.baidu.com/item/%E5%8E%9A%E9%BB%91%E5%AD%A6";
                } else if (position % 5 == 4) {
                    url = "https://baike.baidu.com/item/%E6%A0%BC%E5%B1%80/11034096";
                } else {
                    url = "https://baike.baidu.com/item/2012%E5%B9%B4%E6%96%B0%E6%B5%AA%E4%B8%AD%E5%9B%BD%E5%A5%BD%E4%B9%A6%E6%A6%9C/3140340";
                }

                skipWebActivity(url, "图书");
            }
        });
    }


    /**
     * 轮播图
     */
    private void initBanner() {
        //设置banner样式
        banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
        //设置图片加载器
        banner.setImageLoader(new FresscoImageLoader());
        //设置banner动画效果
//        banner.setBannerAnimation(Transformer.Default);
        //设置自动轮播，默认为true
        banner.isAutoPlay(true);
        //设置轮播时间
        banner.setDelayTime(3000);
        //设置允许手势滑动
        banner.setViewPagerIsScroll(true);
        //设置指示器位置（当banner模式中有指示器时）
        banner.setIndicatorGravity(BannerConfig.CENTER);

        List list_banner = new ArrayList();
        list_banner.add(R.mipmap.icon_book1);
        list_banner.add(R.mipmap.icon_book2);
        list_banner.add(R.mipmap.icon_book3);
        list_banner.add(R.mipmap.icon_book4);
        list_banner.add(R.mipmap.icon_book5);

        banner.setImages(list_banner);

        //banner设置方法全部调用完毕时最后调用
        banner.start();

        banner.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(int position) {
                //banner 图链接，可以随意添加跳转
                String url = "https://www.lib.pku.edu.cn/portal/cn/zy/zzzy/shukanbaozhi";

                skipWebActivity(url, "图书馆");

            }
        });
    }


    public class FresscoImageLoader extends ImageLoader {
        @Override
        public void displayImage(Context context, Object path, ImageView imageView) {
            int url = (int) path;
            imageView.setImageResource(url);
        }

        //提供createImageView 方法，方便fresco自定义ImageView
        @Override
        public ImageView createImageView(Context context) {

            SimpleDraweeView simpleDraweeView = (SimpleDraweeView) getLayoutInflater().inflate(R.layout.layout_banner_imageview, null);
//            SimpleDraweeView simpleDraweeView = new SimpleDraweeView(context);
            return simpleDraweeView;
        }
    }


    @OnClick(R.id.tv_search)
    public void onViewClicked() {
        skipActivity(getActivity(), SearchActivity.class);

    }

    public void skipWebActivity(String web_url, String title) {
        Bundle bundle = new Bundle();
        bundle.putString("url", web_url);
        bundle.putString("title", title);
        skipActivity(getActivity(), MyWebActivity.class, bundle);
    }
}
